package net.esj.auth.inst.struts2.web;

import java.text.MessageFormat;

import net.esj.auth.manager.logger.AdminLogger;
import net.esj.auth.pojo.CoreAdmin;
import net.esj.basic.core.engine.util.logging.LoggerAware;
import net.esj.basic.core.view.tools.AgentContext;
import net.esj.basic.service.provider.UserRuleProvider;

import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
import com.opensymphony.xwork2.util.TextParseUtil;
import com.opensymphony.xwork2.util.ValueStack;

public class LoggerInterceptor extends AbstractInterceptor {

	private AgentContext agentContext;
	
	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		if(invocation.getAction() instanceof LoggerAware){
			LoggerAware aware = (LoggerAware) invocation.getAction();
			if(aware.getLogger() instanceof AdminLogger){
				CoreAdmin admin = UserRuleProvider.getSessionUser(agentContext.getSession(),CoreAdmin.class);
				((AdminLogger)aware.getLogger()).setAdmin(admin);
			}
		}
		return invocation.invoke();
	}

	@Autowired
	public void setAgentContext(AgentContext agentContext) {
		this.agentContext = agentContext;
	}
	
	private static String resolveMessage(String message,ValueStack vs){
		return TextParseUtil.translateVariables(message, vs);
	}
}
